package com.ele.Service.impl;

import com.ele.Dao.DaoImpl.FoodCategoryDaoImpl;
import com.ele.Dao.DaoImpl.ShopDaoImpl;
import com.ele.Dao.FoodCategoryDao;
import com.ele.Dao.ShopDao;
import com.ele.Pojo.Food;
import com.ele.Pojo.FoodCategory;
import com.ele.Pojo.PageResult;
import com.ele.Pojo.Shop;
import com.ele.Pojo.vo.FoodCategoryVO;
import com.ele.Service.FoodCategoryService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodCategoryServiceImpl implements FoodCategoryService {
    //Dao 层
    private FoodCategoryDao foodCategoryDao =new FoodCategoryDaoImpl();
    private ShopDao shopDao = new ShopDaoImpl();


    /**
     * 添加菜品
     * @param categoryName
     * @param shopId
     * @return
     */
    @Override
    public Integer foodCategoryAdd(String categoryName, String shopId) {
        try {
            return foodCategoryDao.foodCategoryAdd(categoryName,shopId);
        }catch (Exception e){
            e.printStackTrace();
        }
       return null;
    }

    /**
     * 删除
     * @param categoryId
     * @return
     */
    public Integer foodCategoryDelete(String categoryId)  {
        try{
            return this.foodCategoryDao.foodCategoryDelete(categoryId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改菜品
     * @param categoryName
     * @param categoryId
     * @return
     */
    public Integer foodCategoryUpdate(String categoryName, String categoryId)  {
        try{
            return this.foodCategoryDao.foodCategoryUpdate(categoryName,categoryId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param shopId
     * @return
     */
    @Override
    public PageResult<List<FoodCategoryVO>> findFoodCategory(String currentPage, String pageSize, String shopId) {
        try {
            //查询到只有菜品分类的数据，其中不包含 菜品所属的店铺名称等
            List<FoodCategory> list = this.foodCategoryDao.findFoodCategory(currentPage, pageSize, shopId);
            List<FoodCategoryVO> foodCategoryVO =new ArrayList<>();
            //遍历当前所有菜品分类的集合
            for (FoodCategory foodCategory : list){
                FoodCategoryVO vo =new FoodCategoryVO();
                Integer sId =foodCategory.getShopId();
                //同店铺的id去查询店铺的信息，需要获取到店铺的名称
                Shop shop =shopDao.findShopById(sId);
                //封装数据
                vo.setShopName(shop.getShopName());
                vo.setCategoryId(foodCategory.getCategoryId());
                vo.setCategoryName(foodCategory.getCategoryName());
                vo.setShopId(foodCategory.getShopId());
                //将封装好的对象放入集合中
                foodCategoryVO.add(vo);

            }
            //查询总记录数据
            Long count =this.foodCategoryDao.foodCategoryCount(shopId);
            PageResult<List<FoodCategoryVO>> pageResult = new PageResult<List<FoodCategoryVO>>();
            pageResult.setTotal(count);
            pageResult.setDatas(foodCategoryVO);
            return pageResult;

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<FoodCategory> findAllFoodCategory(String shopId) {
        try{
            return this.foodCategoryDao.findAllFoodCategory(shopId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }


}
